package org.springframework.context;


import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

/**
 * 获取SpringContext工具类
 * 存储当前初始化的Spring上下文（context）,提供给后续场景使用
 */
@Component( "applicationContextHelper" )
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    public static <T> T getBean(Class<T> clazz){
        if( applicationContext == null){
            return  null;
        }
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz){
        if( applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(name, clazz);
    }
}
